package com.xb.cloud.front.client;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xb.cloud.front.common.FrontSKUService;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.product.category.service.client.bo.SkuRequestBO;

/**
 * 
 * @ClassName: FrontCategoryAdapter
 * @Description:TODO
 * @author: 鲜备科技
 * @date: 2018年1月8日 上午11:10:53
 * 
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class FrontSKUAdapter extends Adapter {

	@Autowired
	FrontSKUService frontSKUService;

	@Override
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo,
			AdapterContext adapterContext) {

		SkuRequestBO requestBO = new SkuRequestBO();
		requestBO.setCompanyID(adapterContext.getCompanyId());
		if (urlPath.trim().toLowerCase().equalsIgnoreCase("/h5/front/sku/list")) {
			requestBO.addCategory(Long.valueOf(bizInfo.get("categoryID")));
			requestBO.setStartPage(Integer.valueOf(bizInfo.get("startPage")));
			requestBO.setPageSize(Integer.valueOf(bizInfo.get("pageSize")));
			requestBO.setSortType(Integer.valueOf(bizInfo.get("sortType")));
			requestBO.setSortOrder(Integer.valueOf(bizInfo.get("sortOrder")));
			RtnInfo skuList = frontSKUService.getSKUListByFrontCategory(requestBO);
			return skuList;
		} else if ("/h5/front/sku/getSkuListByGroup".equalsIgnoreCase(urlPath)) {
			// 根据店铺内的分组获取sku的列表
			Long shopId = null;
			Long groupId = null;
			Integer pageNum = null;
			Integer pageSize = null;
			Integer sortType = null;
			Integer sortOrder = null;
			/**
			 * 参数验证
			 */
			try {
				shopId = Long.parseLong(bizInfo.get("shopId"));
				groupId = StringUtils.isEmpty(bizInfo.get("groupId")) ? null : Long.parseLong(bizInfo.get("groupId"));
				pageNum = Integer.parseInt(bizInfo.get("pageNum"));
				pageSize = Integer.parseInt(bizInfo.get("pageSize"));
				sortType = Integer.parseInt(bizInfo.get("sortType"));
				sortOrder = Integer.parseInt(bizInfo.get("sortOrder"));

				if (shopId == null || pageNum == null || pageSize == null || sortType == null || sortOrder == null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			RtnInfo skuList = frontSKUService.getSkuListByGroup(shopId, groupId, pageNum, pageSize, sortType,
					sortOrder);
			return skuList;
		} else if (urlPath.trim().toLowerCase().equalsIgnoreCase("/h5/front/sku/detail")) {
			String skuID = bizInfo.get("skuID");
			if (StringUtils.isEmpty(skuID)) {
				return ReturnUtils.error(ReturnCodes.SKU_NUM_INVALID);
			}
			requestBO.setSkuID(Long.valueOf(skuID));
			RtnInfo skuDetail = frontSKUService.getSKUDetail(requestBO);
			return skuDetail;
		}else if (urlPath.trim().toLowerCase().equalsIgnoreCase("/h5/front/sku/price")) {
			String skuID = bizInfo.get("skuID");
			if (StringUtils.isEmpty(skuID)) {
				return ReturnUtils.error(ReturnCodes.SKU_NUM_INVALID);
			}
			requestBO.setSkuID(Long.valueOf(skuID));
			RtnInfo skuDetail = frontSKUService.getSKUPrice(requestBO);
			return skuDetail;
		}
		/*
		 * else if
		 * (urlPath.trim().toLowerCase().equals("/h5/category/navigation")) {
		 * RtnInfo navCategory =
		 * frontCategoryService.getNavCategory(adapterContext.getCompanyId());
		 * return navCategory; }
		 */
		return null;
	}

	@Override
	public String getUrlMappingPath() {

		return "/h5/front/sku/**";
	}

}
